package com.terrynoya.fight.stateMachine.triggers
{
	import com.terrynoya.fight.char.Character;
	import com.terrynoya.fight.emuns.Axis;
	import com.terrynoya.fight.emuns.Facing;
	import com.terrynoya.fight.evaluation.Node;
	import com.terrynoya.fight.evaluation.ParseState;
	import com.terrynoya.fight.util.Misc;

	public class P2BodyDist
	{
		public function P2BodyDist()
		{
			
		}
		
		public static function eval(data:Object,node:Node):*
		{
			var chara:Character = Character(data);
			if(chara == null)
			{
				return 0;
			}
			var opponent:Character = chara.getOpponent();
			if(opponent == null)
			{
				return 0;
			}
			
			var axis:int = node.arguments[0];
			switch(axis)
			{
				case Axis.X:
				{
					var myLocation:int = chara.getFrontLocation();
					var oppLocation:int = opponent.getFrontLocation();
					var distance:int = Math.abs(myLocation - oppLocation);
					var rlt:int = 0;
					if(chara.currentFacing == Facing.RIGHT)
					{
						rlt = (opponent.position.x >= chara.position.x) ? distance : -distance;
						trace("P2BodyDist",rlt);
						return rlt;
					}
					else
					{
						rlt = (opponent.position.x >= chara.position.x) ? -distance : distance;
						trace("P2BodyDist",rlt);
						return rlt;
					}
					break;
				}
				case Axis.Y:
				{
					return opponent.position.y - chara.position.y;	
					break;
				}
			}
		}
		
		public static function parse(state:ParseState):Node
		{
			var val:String = state.currentUnknown;
			var axis:int = Misc.getAixs(val);
			if(axis == Axis.None)
			{
				return null;
			}
			state.tokenIndex ++;
			state.baseNode.arguments.push(axis);
			return state.baseNode;
		}
	}
}